package com.qy127.dao.impl;

import com.qy127.dao.BookDao;
import com.qy127.entity.Book;
import com.qy127.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository("bookDaoImpl")
public class BookDaoImpl implements BookDao {


    /**
     * 查询全部，并分页
     * @return
     */
    @Override
    public List<Map<String,Object>> getAllBook(Book book) {

        StringBuffer sql = new StringBuffer("select bookid,bookname,author,count,remark,publisher,price,status,tb.typeid,typename,url from t_book tb,t_type tp where tb.typeid=tp.typeid");
        if (book.getBookname() != null){
            sql.append(" and tb.bookname like '%").append(book.getBookname()+"%'");
        }

        if (book.getAuthor() != null){
            sql.append(" and tb.author like '%").append(book.getAuthor()+"%'");
        }

        //System.out.println(book.getTypeid());

        //由于从页面传回来的 typeid 再经过 BeanUtils.populate(); 封装的时候 变成了0 所以此时 也要判断一下 不等于0 的情况
        if (book.getTypeid() != null && book.getTypeid() !=0){
            sql.append(" and tb.typeid = ").append(book.getTypeid());
        }

        sql.append(" limit ?,?");

        //System.out.println(sql);

        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql.toString(),(book.getPage()-1)*book.getLimit(),book.getLimit());
        return maps;
    }

    /**
     * 查询全部书籍的数量
     * @return
     */
    @Override
    public int getcount(Book book) {
        StringBuffer sql = new StringBuffer("select count(*) from t_book tb where 1=1");
        if (book.getBookname() != null){
            sql.append(" and tb.bookname like '%").append(book.getBookname()+"%'");
        }

        if (book.getAuthor() != null){
            sql.append(" and tb.author like '%").append(book.getAuthor()+"%'");
        }

        if (book.getTypeid() != null && book.getTypeid() !=0){
            sql.append(" and tb.typeid =").append(book.getTypeid());
        }

        List<List> lists = JDBCUtil.queryList(sql.toString());
        Object o = lists.get(0).get(0);
        int i = Integer.parseInt(o + "");
        return i;
    }

    /**
     * 插入一条书籍信息
     * @param book
     * @return
     */
    @Override
    public int insertOneBook(Book book) {

        String sql = "insert into t_book values(null,?,?,?,?,?,?,?,?,?)";
        int i = JDBCUtil.update(sql, book.getBookname(), book.getAuthor(), book.getCount(),
                book.getRemark(), book.getTypeid(), book.getPublisher(),
                book.getPrice(), book.getStatus(),book.getUrl());
        //System.out.println(i+"bookdaoimpl");
        return i;
    }

    /**
     * 通过bookid 修改 status的值
     * 当status的值为1（上架） 的时候 ，就把status的值改为0（下架）
     * 反之亦然
     * @param bookid
     * @param status
     * @return
     */
    @Override
    public int updataStatus(Integer bookid, Integer status) {
        //判断传过来的status的值是否是0，如果是0 则改为1，如果不是就改为0
        if (status == 0) {
            status = 1;
        }else {
            status =0;
        }
        String sql = "update t_book set status =? where bookid =?";
        int i = JDBCUtil.update(sql, status, bookid);
        return i;
    }

    /**
     * 通过bookid修改一条数据
     * @param book
     * @return
     */
    @Override
    public int updateBook(Book book) {
        String sql = "update t_book set bookname=?,author=?,remark=?,typeid=?,publisher=?,price=?,url=? where bookid=?";
        int i = JDBCUtil.update(sql, book.getBookname(), book.getAuthor(), book.getRemark(), book.getTypeid(), book.getPublisher(), book.getPrice(),book.getUrl(),book.getBookid());
        return i;
    }

    /**
     * 通过书名 查询数据
     * @param bookname
     * @return
     */
    @Override
    public Book getOneBook(String bookname) {
        String sql = "select bookname from t_book where bookname =?";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql,bookname);
        if (maps.size()>0){
            Map<String, Object> map = maps.get(0);
            Book book = new Book((String)map.get("bookname"));
            return book;
        }
        return null;
    }

    /**
     * 通过书名和出版社进行验证一本书
     * @param bookname
     * @param publisher
     * @return
     */
    @Override
    public List<Map<String, Object>> queryOneBookByBooknamePublisher(String bookname, String publisher) {
        String sql = "select * from t_book where bookname =? and publisher = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql, bookname, publisher);
        return maps;
    }
}
